﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebAuto
{
    public partial class TestHistoryResults : Form
    {
        private WebAutomation.WebAuto.TestData _testData;

        public TestHistoryResults()
        {
            InitializeComponent();
        }

        public void Init(WebAutomation.WebAuto.TestData testData)
        {
            _testData = testData;
            this.label2.Text = "Viewing history for : "+_testData.TestName;
            _getTestHistory();
        }

        private void _getTestHistory()
        {
            string normalFolderImage = System.Environment.CurrentDirectory + "\\Content\\Images\\folder_icon.png";
            string errorFolderImage = System.Environment.CurrentDirectory + "\\Content\\Images\\folder_icon_error.png";
            this.imageList1.Images.Add(Image.FromFile(normalFolderImage));
            this.imageList1.Images.Add(Image.FromFile(errorFolderImage));
            this.listView1.View = View.Details;
            //this.imageList1.ImageSize = new Size(32, 32);
            this.listView1.SmallImageList = this.imageList1;
            ColumnHeader col1 = new ColumnHeader();
            col1.Text = "Name";
            col1.Width = 180;
            ColumnHeader col2 = new ColumnHeader();
            col2.Text = "Date modified";
            col2.Width = 150;
            this.listView1.Columns.Add(col1);
            this.listView1.Columns.Add(col2);
            if (Directory.Exists(_testData.TestLogFolderLocation))
            {
                var folders =
                    new DirectoryInfo(_testData.TestLogFolderLocation).GetDirectories().OrderByDescending(
                        i => i.LastWriteTime).ToList();
                foreach (var folder in folders)
                {

                    string[] itemValues = new string[2];
                    itemValues[0] = folder.Name;
                    itemValues[1] = folder.LastWriteTime.ToString("dd MMM yyyy HH:mm:ss");

                    ListViewItem item = new ListViewItem(itemValues);
                    if (folder.Name.ToLower().Contains("failed"))
                    {
                        item.ImageIndex = 1;
                    }
                    else
                    {
                        item.ImageIndex = 0;
                    }
                    this.listView1.Items.Add(item);

                }
            }


        }


        private void TestHistoryResults_Load(object sender, EventArgs e)
        {

        }

        private void btnOpenTest_Click(object sender, EventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("notepad.exe");
            startInfo.Arguments = _testData.TestFileName;
            Process.Start(startInfo);
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selItem = listView1.SelectedItems;
            if(selItem!=null)
            {
                string item = selItem[0].Text;
                string folder = _testData.TestLogFolderLocation + "\\" + item;
                ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe");
                startInfo.Arguments = folder;
                Process.Start(startInfo);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
